﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class RewardCardManager : UIPanel
{
    public GameObject cardPattern;

    public GameObject cardContainer;
    
    public GameObject[] cardObjects;

    public PlayerEntityData playerEntityData;
    
    private CardPair[] _cardPairs;
    private int _maxNum;
    
    private bool isShowingUpgrade;

    private GameObject m_belongItem;
    
    
    public void Initial(int maxNum, CardPair[] cardPairs, GameObject belongItem)
    {
        this._maxNum = maxNum;
        this._cardPairs = cardPairs;
        this.m_belongItem = belongItem;
        
        InitializeFromCardPairs();
        SetDisplayText();
    }

    public void InitializeFromCardPairs()
    {

        cardObjects = new GameObject[_cardPairs.Length];
        
        for (int i = 0; i < _cardPairs.Length; i++)
        {
            GameObject cardObject = GameObject.Instantiate(cardPattern, cardContainer.transform, false);
            cardObject.name = $"SelectCard_{i}";
            cardObjects[i] = cardObject;
            
            cardObject.transform.localScale = Vector3.one;
        }
    }
    
    
    public void SetDisplayText()
    {
        for (int i = 0; i < _cardPairs.Length; i++)
        {
            cardObjects[i].GetComponent<CardDisplayerMonobehaviour>().SetPairData(_cardPairs[i]);
        }
    }

    private void Update()
    {
        if (isActive && Input.GetMouseButtonDown(0))
        {
            foreach (RaycastResult result in OnClickGenerate())
            {
                if (result.gameObject.name.Contains("SelectCard"))
                {
                    // TODO 这个就是卡牌了，然后
                    var cardName = result.gameObject.name;
                    int belongCardIndex = Int32.Parse(cardName.Substring(cardName.Length - 1, 1));
                    
                    // 添加到卡牌组里面
                    playerEntityData.currentDeck.Add(_cardPairs[belongCardIndex]);
                    
                    // TODO 然后应该播放动画
                    
                    
                    _maxNum -= 1;
                    Destroy(result.gameObject);
                    
                    // 然后再拿光的时候退出
                    if (_maxNum <= 0)
                    {
                        UiManager.CloseCurrentPanel();    
                    }

                    if (m_belongItem != null)
                    {
                        RewardManager rewardManager = m_belongItem.GetComponent<RewardItem>().rewardManager;
                        Destroy(m_belongItem);

                        if (rewardManager)
                        {
                            rewardManager.rewardItemCount -= 1;
                            rewardManager.ClosePanelWhenZeroList();                            
                        }
                    }
                    
                    GameObject.FindGameObjectWithTag(StaticDataManager.GAME_SESSION_TAG).GetComponent<UIFlusher>().FlushAllUIElement();
                    
                    return;
                }
            }
        }

        CloseWithRightClick();
    }
}
